package com.rpframework.website.device.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseService;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.ICouponDao;
import com.rpframework.website.device.domain.Coupon;

@Service
public class CouponService	extends BaseService{
	public @Resource ICouponDao iDao;
	
	public Pager<Coupon> getPager(Pager<Coupon> pager) {
		long startTime = System.currentTimeMillis();
		List<Coupon> itemList = iDao.doPager(this.packageMyBatisParam(pager));
		pager.setItemList(itemList);
		pager.setCostTime(System.currentTimeMillis()-startTime);
		return pager;
	}
	//删除 过期 的优惠券
	public boolean deleteAll(Integer userId,String nowTime){
		return iDao.deleteAll(userId, nowTime);
	}
	//查询有效期内的优惠券
	public List<Coupon> searchList(Integer userId,String nowTime){
		List<Coupon> list = iDao.searchList(userId, nowTime);
		return list; 
	}
	//查询所有状态正常用户
	//批量插入数据
	public boolean userCoupon(Integer id) {
		if(NumberUtils.isValid(id)){
			Coupon t = iDao.select(id);
			if(t!=null && t.getState()==0 && NumberUtils.isValid(Integer.valueOf(t.getPrice()))){
				t.setState(1);
				return iDao.update(t);
			}else{
				return false;
			}
		}
		return false;
	}
	public Coupon searchById(Integer id) {
		return iDao.select(id);
	}
	/**
	 * 批量发布优惠券
	 * @param list
	 */
	public void doSave(List list) {
		iDao.batchSave(list);
	}
	/**
	 * 加入 备注信息
	 * */
	public JsonElement getJsonInfo() {
		JsonObject json = new JsonObject();
		json.addProperty("name", "优惠券名称");
		json.addProperty("price", "券额");
		json.addProperty("introduction", "简介内容");
		json.addProperty("image", "图片");
		json.addProperty("startTime", "有效开始时间");
		json.addProperty("endTime", "过期时间");
		json.addProperty("userId", "用户id");
		return json;
	}	
}
